package ${pkg}.action.${name};

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import ${pkg}.bean.${upTumTabNam}Bean;
import ${pkg}.bean.io.${name}.${queryIn}Bean;
import ${pkg}.bean.io.${name}.${queryOut}Bean;
import ${pkg}.service.${businessName}Service;

/**
 * @DESCRIBE 查询
 * @AUTHOR LUYU
 */
@Controller
public class Query${businessName}Action implements IBaseAction {
	@Autowired
	private ${businessName}Service service;

	@Override
	@ResponseBody
	@RequestMapping(value = "${queryUrl}", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("${queryIn}");
		${upTumTabNam}Bean beanIn = null;
		if (secIn != null && secIn.size() > 0) {
			JSONObject propIn = secIn.getJSONObject(0);
			${queryIn}Bean ioIn = JSON.toJavaObject(propIn, ${queryIn}Bean.class);
			beanIn = (${upTumTabNam}Bean) BeanUtils.io2Bean(ioIn, ${upTumTabNam}Bean.class, "${prefix}");
		} else {
			beanIn = new ${upTumTabNam}Bean();
		}

		List<${upTumTabNam}Bean> list = service.query(beanIn);

		JSONObject pkgOut = new JSONObject();
		JSONArray secOut = new JSONArray();
		for (${upTumTabNam}Bean ${lowTumTabNam} : list) {
			${queryOut}Bean ioOut = (${queryOut}Bean) BeanUtils.bean2Io(${lowTumTabNam}, ${queryOut}Bean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			<#list globalDefineList as field>
			GlobalDefineUtils.addTransformation(propOut, "${field.lowJavaName}", "${field.globalDefine}");
			</#list>
			secOut.add(propOut);
		}
		pkgOut.put("${queryOut}", secOut);
		return pkgOut;
	}
}
